﻿namespace ACCOUNTING.LVY.Business
{
    using ACCOUNTING;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Data;
    using System.Windows.Forms;

    public class BHXacNhanThanhToan
    {
        private double mDaThanhToan = 0.0;
        private string mKhoa = "";
        private string mKhoaNhapXuat = "";
        private string mLoai = "";
        private string mLoaiTien = "";
        private string mNgayChungTu = "";
        private string mSoChungTu = "";
        private string mSoHoaDon = "";
        private double mTienChietKhau = 0.0;
        private double mTienHang = 0.0;
        private double mTienThanhToan = 0.0;
        private double mTienThue = 0.0;
        private double mTyGia = 0.0;

        public bool DelData(string pKhoa)
        {
            bool flag=false;
            IDataParameter[] commandParameters = new IDataParameter[2];
            try
            {
                commandParameters[0] = DBHelper.createParameter("@Khoa", DbType.Int64, ParameterDirection.Input, this.mKhoa);
                commandParameters[1] = DBHelper.createParameter("@pError", DbType.Double, ParameterDirection.Output, 0);
                DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.StoredProcedure, "BH_sp_XacNhanThanhToanDel", commandParameters);
                if (Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectNotEqual(commandParameters[1].Value, 0, false))
                {
                    return false;
                }
                flag = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public DataTable GetListHoaDon(int IntLoai, string strKhoaKhachHang, string strTuNgay, string strDenNgay, string strKhoaDonVi)
        {
            DataTable table;
            string commandText = "";
            try
            {
                string str2 = " WHERE XB.KhoaDoiTuong = '" + strKhoaKhachHang + "' AND XB.GhiSo = 1 AND XB.KhoaDonVi = '" + strKhoaDonVi.Trim() + "'";
                if (IntLoai == 0)
                {
                    str2 = str2 + " AND (IsNull(XB.TienHangNT,0) - IsNull(XB.TienChietKhauNT,0) + IsNull(XB.TienThueVATNT,0))  > IsNull(XB.DaThanhToanNT,0)  AND XB.NgayChungTu <= '" + strDenNgay + "'";
                    commandText = " SELECT XN.Khoa as Khoa, XB.Khoa as KhoaXuatKho, XB.SoChungTu, dbo.Char2Date(XB.NgayChungTu) As Ngay, XB.SoHoaDon,dbo.Char2Date(XB.NgayThanhToan) as NgayThanhToan,  XB.LoaiTien,XB.TyGia, XB.TienHangNT As TienHang, XB.TienChietKhauNT as TienChietKhau, XB.TienThueVATNT as TienThueVAT, XB.DaThanhToanNT as DaThanhToan,  XB.KhoaTKThanhToan, TK.SoTaiKhoan, XB.KhoaDoiTuong, DT.Ten" + modGeneral.H_LANGUAGE + " as TenDoiTuong,  IsNull(XN.TienThanhToan,0) as ThanhToan  FROM ST_XuatKho XB LEFT JOIN DM_TaiKhoan TK on XB.KhoaTKThanhToan = TK.Khoa  LEFT JOIN DM_DoiTuong DT on XB.KhoaDoiTuong = DT.Khoa  LEFT JOIN BH_HoaDonThanhToan XN on XB.Khoa = XN.KhoaNhapXuat " + str2 + " ORDER BY XB.NgayChungTu, XB.SoChungTu";
                }
                if (IntLoai == 1)
                {
                    str2 = str2 + " AND ( XB.NgayChungTu Between '" + strTuNgay + "' And '" + strDenNgay + "') ";
                    commandText = " SELECT XN.Khoa, XB.Khoa as KhoaXuatKho, XB.SoChungTu, dbo.Char2Date(XB.NgayChungTu) As Ngay, XB.SoHoaDon,dbo.Char2Date(XB.NgayThanhToan) as NgayThanhToan,  XB.LoaiTien,XB.TyGia, XB.TienHangNT AS TienHang, XB.TienChietKhauNT As TienChietKhau, XB.TienThueVATNT as TienThueVAT, XB.DaThanhToanNT as DaThanhToan,  XB.KhoaTKThanhToan, TK.SoTaiKhoan, XB.KhoaDoiTuong, DT.Ten" + modGeneral.H_LANGUAGE + " as TenDoiTuong,  IsNull(XN.TienThanhToan,0) as ThanhToan  FROM BH_HoaDonThanhToan XN left join ST_XuatKho XB on XN.KhoaNhapXuat = XB.Khoa  LEFT JOIN DM_TaiKhoan TK on XB.KhoaTKThanhToan = TK.Khoa  LEFT JOIN DM_DoiTuong DT on XB.KhoaDoiTuong = DT.Khoa " + str2 + " ORDER BY XB.NgayChungTu, XB.SoChungTu";
                }
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message);
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public DataTable GetListHoaDonNhap(int IntLoai, string strKhoaNhaCungCap, string strTuNgay, string strDenNgay, string strKhoaDonVi)
        {
            DataTable table;
            string commandText = "";
            string str2 = "";
            try
            {
                str2 = " WHERE XB.KhoaDoiTuong = '" + strKhoaNhaCungCap + "' AND XB.GhiSo = 1 AND XB.KhoaDonVi = '" + strKhoaDonVi.Trim() + "'";
                if (IntLoai == 0)
                {
                    str2 = str2 + " AND ((IsNull(XB.TienHangNT,0) - IsNull(XB.TienChietKhauNT,0) + IsNull(XB.TienThueVATNT,0)) - IsNull(XB.DaThanhToanNT,0)) > 0  AND XB.NgayChungTu <= '" + strDenNgay + "'";
                    commandText = " SELECT XN.Khoa as Khoa, XB.Khoa as KhoaNhapXuat, XB.SoChungTu, dbo.Char2Date(XB.NgayChungTu) As Ngay, XB.SoHoaDon,dbo.Char2Date(XB.NgayThanhToan) as NgayThanhToan,  XB.LoaiTien,XB.TyGia, XB.TienHangNT As TienHang, XB.TienChietKhauNT as TienChietKhau,  TienThueVAT = Case XB.NguonNhap WHEN 'NK' THEN 0 WHEN 'TN' THEN XB.TienThueVATNT END , XB.DaThanhToanNT,  XB.KhoaTKThanhToan, TK.SoTaiKhoan, XB.KhoaDoiTuong, DT.Ten" + modGeneral.H_LANGUAGE + " as TenDoiTuong,  IsNull(XN.TienThanhToan,0) as ThanhToan  FROM ST_NhapKho XB LEFT JOIN DM_TaiKhoan TK on XB.KhoaTKThanhToan = TK.Khoa  LEFT JOIN DM_DoiTuong DT on XB.KhoaDoiTuong = DT.Khoa  LEFT JOIN BH_HoaDonThanhToan XN on XB.Khoa = XN.KhoaNhapXuat " + str2 + " ORDER BY XB.NgayChungTu, XB.SoChungTu";
                }
                if (IntLoai == 1)
                {
                    str2 = str2 + " AND ( XB.NgayChungTu Between '" + strTuNgay + "' And '" + strDenNgay + "') ";
                    commandText = " SELECT XN.Khoa as Khoa, XB.Khoa as KhoaNhapXuat, XB.SoChungTu, dbo.Char2Date(XB.NgayChungTu) As Ngay, XB.SoHoaDon,dbo.Char2Date(XB.NgayThanhToan) as NgayThanhToan,  XB.LoaiTien,XB.TyGia, XB.TienHangNT As TienHang, XB.TienChietKhauNT as TienChietKhau,  TienThueVAT = Case XB.NguonNhap WHEN 'NK' THEN 0 WHEN 'TN' THEN XB.TienThueVATNT END , XB.DaThanhToanNT,  XB.KhoaTKThanhToan, TK.SoTaiKhoan, XB.KhoaDoiTuong, DT.Ten" + modGeneral.H_LANGUAGE + " as TenDoiTuong,  IsNull(XN.TienThanhToan,0) as ThanhToan  FROM BH_HoaDonThanhToan XN LEFT JOIN ST_NhapKho XB ON XN.KhoaNhapXuat = XB.Khoa LEFT JOIN DM_TaiKhoan TK on XB.KhoaTKThanhToan = TK.Khoa  LEFT JOIN DM_DoiTuong DT on XB.KhoaDoiTuong = DT.Khoa " + str2 + " ORDER BY XB.NgayChungTu, XB.SoChungTu";
                }
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message);
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public bool Load(string pKhoa)
        {
            bool flag=false;
            try
            {
                string commandText = "SELECT * FROM BH_HoaDonThanhToan WHERE Khoa = '" + pKhoa + "'";
                IDataReader dataReader = DBHelper.ExecuteReader(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]);
                if (dataReader.Read())
                {
                    this.mKhoa = Conversions.ToString(modGeneral.GetValue(dataReader, 0, modGeneral.typeOfColumn.GetString));
                    this.mKhoaNhapXuat = Conversions.ToString(modGeneral.GetValue(dataReader, 1, modGeneral.typeOfColumn.GetString));
                    this.mSoChungTu = Conversions.ToString(modGeneral.GetValue(dataReader, 2, modGeneral.typeOfColumn.GetString));
                    this.mNgayChungTu = Conversions.ToString(modGeneral.GetValue(dataReader, 3, modGeneral.typeOfColumn.GetString));
                    this.mSoHoaDon = Conversions.ToString(modGeneral.GetValue(dataReader, 4, modGeneral.typeOfColumn.GetString));
                    this.mLoaiTien = Conversions.ToString(modGeneral.GetValue(dataReader, 5, modGeneral.typeOfColumn.GetString));
                    this.mTyGia = Conversions.ToDouble(modGeneral.GetValue(dataReader, 6, modGeneral.typeOfColumn.GetDecimal));
                    this.mTienHang = Conversions.ToDouble(modGeneral.GetValue(dataReader, 7, modGeneral.typeOfColumn.GetDecimal));
                    this.mTienChietKhau = Conversions.ToDouble(modGeneral.GetValue(dataReader, 8, modGeneral.typeOfColumn.GetDecimal));
                    this.mTienThue = Conversions.ToDouble(modGeneral.GetValue(dataReader, 9, modGeneral.typeOfColumn.GetDecimal));
                    this.mDaThanhToan = Conversions.ToDouble(modGeneral.GetValue(dataReader, 10, modGeneral.typeOfColumn.GetDecimal));
                    this.mTienThanhToan = Conversions.ToDouble(modGeneral.GetValue(dataReader, 11, modGeneral.typeOfColumn.GetDecimal));
                    this.mLoai = Conversions.ToString(modGeneral.GetValue(dataReader, 12, modGeneral.typeOfColumn.GetString));
                }
                dataReader.Close();
                flag = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public bool Save()
        {
            bool flag=false;
            IDataParameter[] commandParameters = new IDataParameter[14];
            try
            {
                commandParameters[0] = DBHelper.createParameter("@Khoa", DbType.Int64, ParameterDirection.Input, this.mKhoa);
                commandParameters[1] = DBHelper.createParameter("@KhoaNhapXuat", DbType.Int64, ParameterDirection.Input, this.mKhoaNhapXuat);
                commandParameters[2] = DBHelper.createParameter("@SoChungTu", DbType.Int64, ParameterDirection.Input, this.mSoChungTu);
                commandParameters[3] = DBHelper.createParameter("@NgayChungTu", DbType.Int64, ParameterDirection.Input, this.mNgayChungTu);
                commandParameters[4] = DBHelper.createParameter("@SoHoaDon", DbType.Int64, ParameterDirection.Input, this.mSoHoaDon);
                commandParameters[5] = DBHelper.createParameter("@LoaiTien", DbType.Int64, ParameterDirection.Input, this.mLoaiTien);
                commandParameters[6] = DBHelper.createParameter("@TyGia", DbType.Date, ParameterDirection.Input, this.mTyGia);
                commandParameters[7] = DBHelper.createParameter("@TienHang", DbType.Date, ParameterDirection.Input, this.mTienHang);
                commandParameters[8] = DBHelper.createParameter("@TienChietKhau", DbType.Date, ParameterDirection.Input, this.mTienChietKhau);
                commandParameters[9] = DBHelper.createParameter("@TienThue", DbType.Date, ParameterDirection.Input, this.mTienThue);
                commandParameters[10] = DBHelper.createParameter("@DaThanhToan", DbType.Date, ParameterDirection.Input, this.mDaThanhToan);
                commandParameters[11] = DBHelper.createParameter("@TienThanhToan", DbType.Date, ParameterDirection.Input, this.mTienThanhToan);
                commandParameters[12] = DBHelper.createParameter("@Loai", DbType.Int64, ParameterDirection.Input, this.mLoai);
                commandParameters[13] = DBHelper.createParameter("@pError", DbType.Double, ParameterDirection.Output, 0);
                DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.StoredProcedure, "BH_sp_XacNhanThanhToan", commandParameters);
                if (Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectNotEqual(commandParameters[13].Value, 0, false))
                {
                    return false;
                }
                flag = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public double DaThanhToan
        {
            get
            {
                return this.mDaThanhToan;
            }
            set
            {
                this.mDaThanhToan = value;
            }
        }

        public string Khoa
        {
            get
            {
                return this.mKhoa;
            }
            set
            {
                this.mKhoa = value;
            }
        }

        public string KhoaNhapXuat
        {
            get
            {
                return this.mKhoaNhapXuat;
            }
            set
            {
                this.mKhoaNhapXuat = value;
            }
        }

        public string Loai
        {
            get
            {
                return this.mLoai;
            }
            set
            {
                this.mLoai = value;
            }
        }

        public string LoaiTien
        {
            get
            {
                return this.mLoaiTien;
            }
            set
            {
                this.mLoaiTien = value;
            }
        }

        public string NgayChungTu
        {
            get
            {
                return this.mNgayChungTu;
            }
            set
            {
                this.mNgayChungTu = value;
            }
        }

        public string SoChungTu
        {
            get
            {
                return this.mSoChungTu;
            }
            set
            {
                this.mSoChungTu = value;
            }
        }

        public string SoHoaDon
        {
            get
            {
                return this.mSoHoaDon;
            }
            set
            {
                this.mSoHoaDon = value;
            }
        }

        public double TienChietKhau
        {
            get
            {
                return this.mTienChietKhau;
            }
            set
            {
                this.mTienChietKhau = value;
            }
        }

        public double TienHang
        {
            get
            {
                return this.mTienHang;
            }
            set
            {
                this.mTienHang = value;
            }
        }

        public double TienThanhToan
        {
            get
            {
                return this.mTienThanhToan;
            }
            set
            {
                this.mTienThanhToan = value;
            }
        }

        public double TienThue
        {
            get
            {
                return this.mTienThue;
            }
            set
            {
                this.mTienThue = value;
            }
        }

        public double TyGia
        {
            get
            {
                return this.mTyGia;
            }
            set
            {
                this.mTyGia = value;
            }
        }
    }
}

